/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mavenrentcars.controller;

import com.mavenrentcars.exception.ViewException;
import com.mavenrentcars.form.AccountForm;
import com.mavenrentcars.model.Role;
import com.mavenrentcars.service.AccountService;
import com.mavenrentcars.service.RoleService;
import com.mavenrentcars.validator.Validator;
import com.mavenrentcars.validator.impl.AccountValidator;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletResponse;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author serg
 */
@Controller
public class AccountController {
    
    @Autowired
    AccountService accountService;

    @Autowired
    RoleService roleService;
    
    @RequestMapping(value = "account_item.htm", method = RequestMethod.GET)
	public ModelAndView item(
                HttpServletResponse httpServletResponse) {
            ModelAndView mav = new ModelAndView("account/item");
            
                AccountForm form = new AccountForm(accountService.getLoggedInAccount());
                form.setAction("edit");
                try {
                    List<Role> roles = roleService.getAll();
                    String json_roles = roleService.getJsonRoles();
                    mav.addObject("roles", roles);
                    mav.addObject("json_roles", json_roles);
                } catch (ViewException ex) {
                    Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, ex);
                }
                mav.addObject("item", form);
                mav.addObject("acc", accountService);
                mav.addObject("res", true);
            return mav;
        }
    
    @RequestMapping(value = "account_view.htm", method = RequestMethod.GET)
	public ModelAndView view(
                HttpServletResponse httpServletResponse) {
            ModelAndView mav = new ModelAndView("account/item");
            
                AccountForm form = new AccountForm();
                try {
                    List<Role> roles = roleService.getAll();
                    String json_roles = roleService.getJsonRoles();
                    mav.addObject("roles", roles);
                    mav.addObject("json_roles", json_roles);
                } catch (ViewException ex) {
                    Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, ex);
                }
                mav.addObject("item", form);
                mav.addObject("acc", accountService);
                mav.addObject("res", true);
            return mav;
        }
    
    @RequestMapping(value = "account_save.htm", method = RequestMethod.POST)
	public ModelAndView save(
            //AccountForm form,
            HttpServletResponse httpServletResponse,
            @RequestParam(value = "data", required = true) String data) {

            ObjectMapper mapper = new ObjectMapper();
            ModelAndView mav = new ModelAndView("jsonView");

            AccountForm form;
            try {
                form = mapper.readValue(data, AccountForm.class);
                Validator valid = new AccountValidator(form);
                if (!valid.validation()) {
                    mav.addObject("res", false);
                    mav.addObject("errors", valid.getErrors());
                    return mav;
                }

                accountService.saveAccount(form);
                mav.addObject("res", true);
                mav.addObject("item", form);

            } catch (ViewException ex) {
                mav.addObject("error", ex.getMessage());
                mav.addObject("res", false);
            } catch (JsonGenerationException ex) {
               mav.addObject("error", ex.getMessage());
                mav.addObject("res", false);
            } catch (JsonMappingException ex) {
                mav.addObject("error", ex.getMessage());
                mav.addObject("res", false);
            } catch (IOException ex) {
                mav.addObject("error", ex.getMessage());
                mav.addObject("res", false);
            }
            return mav;
        }
    
}
